package com.diandi.log;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;

/**
 * controller 拦截参数校验/ feign传递信息获取
 * Created by 袁恩光 on 2018/1/5.
 */
@Component
@Aspect
@Order(1)
public class LogRequestAspect {

    /**
     * 默认参数 userId
     */
    private static final String USER_ID = "userId";
    /**
     * 默认参数 当前IP
     */
    private static final String HOST_ADDRESS = "hostAddress";

    @Autowired
    private LogProperties logProperties;

    /**
     * 信息拦截
     */
    @Around("@within(restController)")
    public Object requestAspect(ProceedingJoinPoint joinPoint, RestController restController) throws Throwable {
        ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
        HttpServletResponse res = servletRequestAttributes.getResponse();
        LinkedHashMap<String, String> mapLog = new LinkedHashMap<>();
        LogTemp.LOG_TITLE_MESSAGE.set(mapLog);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //判断是否是feign 携带的log信息
        if (null == request.getParameter(LogTemp.NEXT_LOG_DATA)) {
            for (String str : logProperties.getParams()) {
                String reqParam = request.getParameter(str);
                mapLog.put(str, reqParam);
            }
            mapLog.put(USER_ID, request.getParameter(USER_ID));
        } else {
            mapLog = JSONObject.parseObject(request.getParameter(LogTemp.NEXT_LOG_DATA), LinkedHashMap.class);
        }
        //TODO 加入服务IP
        LogTemp.LOG_TITLE_MESSAGE.set(mapLog);
        Object obj = null;
        try {
            obj = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            res.setHeader("Content-type", "text/html;charset=UTF-8");
            res.getWriter().write(JSONObject.toJSONString(new Result(-1, "服务器异常", throwable.toString())));
            return null;
        }
        return obj;
    }


    class Result {

        /**
         * 操作code
         */
        private Integer code;
        /**
         * 返回提示信息
         */
        private String msg;
        /**
         * 返回实体，可包含各种信息
         */
        private Object obj;

        public Result() {
            super();

        }

        public Result(Integer code, String msg) {
            super();
            this.code = code;
            this.msg = msg;
        }


        public Result(Integer code, String msg, Object obj) {
            super();
            this.code = code;
            this.msg = msg;
            this.obj = obj;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }


    }
}
